const util = require("/util/util");
const requsts = require("/util/requst");
Page({
  data: {
    name:"",
    tell:"",
    addres:"",
    checked:0,
    clear:true,
    userId:"",
    shippingaddress:"",  //收货地址
    detailedaddress:"",   //详细地址
  },
  onItemInput(e){
    this.setData({
      [e.target.dataset.field]: e.detail.value,
    });
  },
  onClear(e){
       this.setData({
      [e.target.dataset.field]: '',
    });
  },
  focusfn(){
    my.multiLevelSelect({
        title: '多级联选择器',//级联选择标题
        list: [
        {
            name: "杭州市",//条目名称
            subList: [
                {
                    name: "西湖区",
                    subList: [
                        {
                            name: "古翠街道"
                        },
                        {
                            name: "文新街道"
                        }
                    ]
                },
                {
                    name: "上城区",
                    subList: [
                        {
                            name: "延安街道"
                        },
                        {
                            name: "龙翔桥街道"
                        }
                    ]
                }
            ]//级联子数据列表
        }],//级联数据列表
        success:(res)=>{
            my.alert({title:JSON.stringify(res)})
        }
    });
  },
  //地图
  onExtraTap(){
    var that = this
    my.chooseLocation({
         success:(res)=>{
          console.log(JSON.stringify(res))
          //my.alert({content:JSON.stringify(res)})
          //my.alert({content:JSON.stringify(res.provinceName+res.cityName+res.adName)})
          if(res.provinceName==undefined&&res.cityName==undefined&&res.adName==undefined){
            that.setData({
              detailedaddress:res.address
            })
          }else{
            that.setData({
              shippingaddress:(res.provinceName+res.cityName+res.adName),
              detailedaddress:res.address
            })
          }
          
        },
        fail:(error)=>{
          //my.alert({content: '调用失败：'+JSON.stringify(error), });
        },
    });
  },
  onBlur(e){
      this.setData({
        detailedaddress:e.detail.value
      })
      if(!this.data.detailedaddress){
       util.toastfn("请填写详细地址")
       return
     }
  }, 
  submit(){
     if(!this.data.name){
       util.toastfn("请填写收货人姓名")
       return
     }
     if(!this.data.tell){
       util.toastfn("请输入手机号码")
       return
     }
     if(this.data.tell.length<11){
       util.toastfn("请输入正确手机号码")
       return
     }
     if(!util.isPositiveInteger(this.data.tell)){
       util.toastfn("请输入正确手机号码")
       return
     }
     if(!this.data.shippingaddress){
       util.toastfn("请填写收货地址")
       return
     }
    //  var param="userId="+this.data.userId+"&shippingName="+this.data.name+"&shippingPhone="+this.data.tell+
    //  "&shippingAddress="+this.data.shippingaddress+"&detailedAddress="+this.data.detailedaddress+"&isDefault="+
    //  this.data.checked
    var param={
      userId:this.data.userId,
      shippingName:this.data.name,
      shippingPhone:this.data.tell,
      shippingAddress:this.data.shippingaddress,
      detailedAddress:this.data.detailedaddress,
      isDefault:this.data.checked,
    }
     console.log(param)
    my.request({
      url: requsts.url+"/user/addShippingInfo",
      method: 'POST',
      headers: {'content-type': 'application/x-www-form-urlencoded'},
      data:param,
      dataType: 'json',
      success: function(res) {
        console.log(res)
        if(res.data.code==0){
            my.navigateTo({
              url: '/page/common/myaddress/myaddress',
            });
        }else{
          util.toastfn(res.data.msg)
        }
      },
      fail: function(res) {
          util.toastfn(res.errorMessage)
      },
    });
  },
  switch2Change(e){
    if(e.detail.value){
        this.setData({
       checked:1
     })
    }else{
      this.setData({
       checked:0
     })
    }
    //  this.setData({
    //    checked:e.detail.value
    //  })
     console.log(this.data.checked)
  },
  onLoad(){
    var that=this
    my.getStorage({
      key: 'userId',
      success: function(res) {
        that.setData({
          userId:res.data.userId,
        })
      },
      fail: function(res){
        util.toastfn(res.errorMessage)
      }
    });
  },
});